<?php

class CliCalc
{
	private $context;
	private $statements;
	private $output;

	public function __construct (CliContext $context)
	{
		$this->context = $context;
		$this->statements = array();
		$this->output = array();
	}

	public function loadInput ($input)
	{
		try
		{
			$parser = new CliParser($this->context->getMode());
			$statements = $parser->parseInput($input);
			$this->statements = $statements;
		}
		catch (CliParserException $ex)
		{
			$this->output = array($ex->getMessage());
			return false;
		}

		return true;
	}

	public function runInput ()
	{
		try
		{
			foreach ($this->statements as $key => $stmt)
			{
				$this->output[] = $stmt->execute($this->context);
			}
		}
		catch (CliEvaluationException $ex)
		{
			$this->output[] = $ex->getMessage();
			return false;
		}

		return true;
	}

	public function getOutput ()
	{
		return $this->output;
	}

	public function getContext ()
	{
		return $this->context;
	}
}

?>